using UnityEngine;
using QFramework;
using ProjectSurvivor;
using QAssetBundle;

namespace QFrameworkSurvivor
{
	public partial class Enemy : ViewController, IEnemy
	{
		public float speed = 2;
		public float HP = 3;
        public Color DissolveColor = Color.red;
        public Texture Texture;
        public bool TreasureChestEnemy = false;
        void Start()
		{
			// Code Here
			EnemyGenerator.EnemyCount.Value++;
		}
        private void FixedUpdate()
        {
            if(!mIgnoreHurt)
            {
                if (Player.Default)
                {
                    var direction = (Player.Default.transform.position - transform.position).normalized;
                    SelfRigidbody2D.velocity = direction * speed;
                }
                else
                {
                    SelfRigidbody2D.velocity = Vector2.zero;
                }
            }
        }
        void Update()
		{
			if(HP<=0)
			{
                //UIKit.OpenPanel<UIGamePassPanel>();
                AudioKit.PlaySound(Sfx.ENEMYDIE);
                Global.GeneratePowerUp(gameObject, TreasureChestEnemy);
                FxController.Play(Sprite,DissolveColor,Texture);
                this.DestroyGameObjGracefully();
			}
		}
        private void OnDestroy()
        {
            EnemyGenerator.EnemyCount.Value--;
        }
        private bool mIgnoreHurt = false;
        public void Hurt(float v,bool force=false,bool critical=false)
		{
			if (mIgnoreHurt&&!force&&gameObject!=null) return;
            mIgnoreHurt=true;
            SelfRigidbody2D.velocity=Vector2.zero;
            FloatingTextController.Play(transform.position+Vector3.up,v.ToString("0.0"),critical);
            Sprite.color = Color.red;
            ActionKit.Delay(0.2f, () =>
            {
                mIgnoreHurt = false;
                HP -= v;
                Sprite.color = Color.white;
            }).Start(this);
        }

        public void SetHpScale(float hpScale)
        {
            HP*=hpScale;
        }

        public void SetSpeedScale(float speedScale)
        {
            speed*=speedScale;
        }
    }
}
